package com.yang.service;

import com.yang.entity.Cost;
import com.yang.entity.Details;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.UUID;

@Service
public class DetailsService {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    public void saveBatch(List<Details> list) {
        for (Details details : list) {
            double total=0.00;
            String[] split = details.getTime().split("-");
            String year=split[0];
            String month=split[2];
            String toWhere = details.getToWhere().substring(0,2);
            String remark="计算公式:";
            int ceil = new Double(Math.ceil(details.getWeight())).intValue();
            String sql = "select to_where, first_weight , first_price, add_price from cost where to_where like ?";
            Cost model=jdbcTemplate.queryForObject(sql, new Object[]{toWhere+"%"},
                    new BeanPropertyRowMapper<>(Cost.class));

            if(ceil<=model.getFirstWeight()){
                if(ceil<=5){
                    total=total+model.getFirstPrice()+0.1;
                    total=formatDouble(total);
                    remark=remark+"重量小于等于首重,首重价格+有偿派件费:"+model.getFirstPrice()+"+0.10"+"="+total;
                }else{
                    total=total+model.getFirstPrice()+0.1+0.1*ceil;
                    total=formatDouble(total);
                    remark=remark+"重量小于等于首重,首重价格+有偿派件费+超重费:"+model.getFirstPrice()+"+0.1"+"+0.1*"+ceil+"="+total;
                }

            }else{
                if(ceil<=5){
                   total=0.10+model.getFirstPrice()+(ceil-model.getFirstWeight())*model.getAddPrice();
                    total=formatDouble(total);
                   remark=remark+"重量大于首重,首重价格+(重量减去首重)*续重+收有偿派件费:"+model.getFirstPrice()+"+("+ ceil+"-"+ model.getFirstWeight()+")*"+ model.getAddPrice()+"+0.1"+"=" +total;
                }else{
                    total=0.10+model.getFirstPrice()+(ceil-model.getFirstWeight())*model.getAddPrice()+0.10*ceil;
                    total=formatDouble(total);
                    remark=remark+"重量大于首重,首重价格+(重量减去首重)*续重+收有偿派件费+超重费:"+model.getFirstPrice()+"+("+ ceil+"-"+ model.getFirstWeight()+")*"+ model.getAddPrice()+"+0.1"+"" +
                            "+0.1*"+ceil+"=" +total;
                }
            }
            System.out.println(remark);
            String id=UUID.randomUUID().toString();
            String insertSql=" insert into details (  id, year, month, send_no, ower, to_where, remark, weight, cost,total ) values ("+"'"+id+"',"
                    +"'"+year+"',"+"'"+month+"',"+"'"+details.getSendNo()+"',"+"'"+details.getOwer()+"',"+"'"+details.getToWhere()+"',"+"'"+remark+"',"
                    +details.getWeight()+","+details.getPrice()+","+total+")";

            jdbcTemplate.execute(insertSql);


        }

    }
    public static double formatDouble(double d) {
        return (double)Math.round(d*100)/100;
    }

}
